package lc.activiti.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lc.activiti.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ServiceFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		  log.info("-------->>>>Request Begin>>>>---------->");
	        long startTime = System.currentTimeMillis();
	        // 防止流读取一次后就没有了, 所以需要将流继续写出去
	        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
	        ServletRequest requestWrapper = new LCHttpServletRequestWrapper(httpServletRequest);
	        log.info("RemoteHost={}", httpServletRequest.getRemoteHost());
	        String method = httpServletRequest.getMethod();
	        log.info("Method={}", method);
	        String url = httpServletRequest.getRequestURL().toString();
	        log.info("URL={}", url);
	        /*//过滤，传图片的直接放过！！！
	        for (String s : noNeedToFilt) {
	            if (-1 != url.indexOf(s)) {
	                log.info("属于信任url直接放过");
	                log.info("<--------<<<<Request End<<<<----------");
	                chain.doFilter(request, response);
	                return;
	            }
	        }*/
	        String queryString = httpServletRequest.getQueryString();
	        log.info("QueryString={}", queryString);
	        Map<String, String> headersInfo = HttpUtils.getHeadersInfo(httpServletRequest);
	        log.info("Headers={}", headersInfo);
	        String json = HttpUtils.getBodyString(requestWrapper);
	        log.info("Body={}", json);
	        response.setCharacterEncoding("UTF-8");
	        response.setContentType("application/json; charset=utf-8");
	        String remoteHost = httpServletRequest.getRemoteHost();
	        String remoteXRealHost = httpServletRequest.getHeader("x-real-ip");
	        log.info("请求IP: remoteHost={},remoteXRealHost={}", remoteHost, remoteXRealHost);
	        
	        
	        ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response);
	        //处理业务
	        chain.doFilter(requestWrapper, responseWrapper);
	        
	        long endTime = System.currentTimeMillis();
	        // 获取response返回的内容并重新写入response
	        String result = responseWrapper.getResponseData(response.getCharacterEncoding());
	        response.getOutputStream().write(result.getBytes());

	        String uri = httpServletRequest.getRequestURI();
	        log.info("URL={}", url);
	        log.info("ResponseCode={}", String.valueOf(responseWrapper.getStatus()));
	        log.info("ResponseString={}", result);
	        log.info("TimeConsuming={}", (int) (endTime - startTime));

	        log.info("<--------<<<<Request End<<<<----------");
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
